<%@page import="wf3.kernel.WorkflowFactory"%>
<%@page import="wf.dto.pbarcadj"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="wf.dto.pbins_arcadj"%>
<%@page import="org.hibernate.Query"%>
<%@page import="org.hibernate.StatelessSession"%>
<%@page import="acceso.hibernate.AccesoHibernate"%>
<%@page import="acceso.util.Escritor"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%
    int co_imagen = 0;
    String file = "";
    try {
        co_imagen = Integer.parseInt(request.getParameter("file"));
    } catch (Exception e) {
        file = request.getParameter("file");
    }

    String tipo_pagina = request.getParameter("tipo");
    FileInputStream fis = null;
    File fout = null;
    boolean existe = true;
    String filename = "";

    if (co_imagen != 0) {
        try {
            StatelessSession HSESSION = AccesoHibernate.getSessionFactory().openStatelessSession();
            pbarcadj arcadj;

            try {
                Query HQUERY = null;
                HQUERY = HSESSION.getNamedQuery("arcadj.pbarcadj");
                HQUERY.setInteger("p_id_arcadj", co_imagen);
                Escritor.escribe_frawor(HQUERY.getQueryString().replaceAll(":p_id_arcadj", "" + co_imagen));
                arcadj = (pbarcadj) HQUERY.uniqueResult();
                HSESSION.close();
            } catch (Exception ep) {
                Escritor.escribe_errors(ep.getMessage() + ":" + ep.getClass().toString());
                if (HSESSION != null) {
                    HSESSION.close();
                }
                throw new Exception("ERROR");
            }

            filename = arcadj.getId_arcadj() + "." + arcadj.getNo_arcadj().split("\\.(?=[^\\.]+$)")[1];
            fis = new FileInputStream(WorkflowFactory.getConfiguration().getFiles_path() + "/" + new SimpleDateFormat("yyyy/MM/dd").format(arcadj.getFe_arcadj()) + "/" + filename);
            Escritor.escribe_debug("" + getServletConfig().getServletContext().getRealPath(""));
            fout = new File(getServletConfig().getServletContext().getRealPath("") + "/images/documentos_dia/" + filename);
            FileOutputStream fos = new FileOutputStream(fout, false);

            byte[] buffer = new byte[8192];
            int bytesLeidos = 0;
            while ((bytesLeidos = fis.read(buffer, 0, 8192)) != -1) {
                fos.write(buffer, 0, bytesLeidos);
            }
            fos.close();
        } catch (Exception ex) {
            Escritor.escribe_errors("Archivo no encontado:" + ex.getMessage());
            existe = false;
        }
    } else {
        try {
            fis = new FileInputStream(WorkflowFactory.getConfiguration().getFiles_path() + "/" + file.replaceAll("WFACR/DOCUMENTOS/", ""));
            filename = file.substring(file.lastIndexOf('/') + 1);
            fout = new File(getServletConfig().getServletContext().getRealPath("") + "/images/documentos_dia/" + filename);
            FileOutputStream fos = new FileOutputStream(fout, false);

            byte[] buffer = new byte[8192];
            int bytesLeidos = 0;
            while ((bytesLeidos = fis.read(buffer, 0, 8192)) != -1) {
                fos.write(buffer, 0, bytesLeidos);
            }
            fos.close();
        } catch (Exception ex) {
            Escritor.escribe_errors("Archivo no encontado:" + ex.getMessage());
            existe = false;
        }
    }


%>
<html>
    <head>        
        <meta http-equiv="expires" content="<%= new java.util.Date()%>"> 
        <meta http-equiv="pragma" content="no-cache"> 
        <meta http-equiv="no-cache">
        <title><%=filename%></title>
        <link rel="shortcut icon" href="${pageContext.request.contextPath}/system/imagenes/${fn:toLowerCase(CFG.no_system)}/favicon.ico">
    </head>
    <body>    

        <%
            if (existe) {
                response.sendRedirect("../images/documentos_dia/" + filename);
            } else {
        %>
        Archivo no encontrado.
        <%
            }
        %>

    </body>
</html>
